package com.vincce.sport.core.view;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: henry
 * Date: 8/14/13
 * Time: 4:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class FastJsonJsonView extends com.alibaba.fastjson.support.spring.FastJsonJsonView {

	private Set<String>         renderedAttributes;

	public void setRenderedAttributes(Set<String> renderedAttributes) {
		this.renderedAttributes = renderedAttributes;
	}

	protected Object filterModel(Map<String, Object> model) {
		Map<String, Object> result = new HashMap<String, Object>(model.size());
		Set<String> renderedAttributes = !CollectionUtils.isEmpty(this.renderedAttributes) ? this.renderedAttributes : model.keySet();
		for (Map.Entry<String, Object> entry : model.entrySet()) {
			if (entry.getValue() != null && !(entry.getValue() instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) {
				result.put(entry.getKey(), entry.getValue());
			}
		}
		if (result.size() == 1 && result.containsKey("$$skip$$")) {
			return result.get("$$skip$$");
		}
		if (result.size() > 1) {
			result.remove("$$skip$$");
		}
		return result;
	}

}